<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id="message">
    <button id="sendButton">发送</button>
</body>
</html>


<script>
    let websocket = new WebSocket("ws://127.0.0.1:8080/test");

    websocket.onopen = function() {
        console.log("open!");
    }

    websocket.onclose = function() {
        console.log("close!");
    }

    websocket.onerror = function() {
        console.log("error!");
    }

    websocket.onmessage = function(e) {
        console.log("recv: " + e.data);
    }

    let messageInput = document.querySelector("#message");
    let sendButton = document.querySelector("#sendButton");
    sendButton.onclick = function() {
        console.log("send: " + messageInput.value);
        websocket.send(messageInput.value);
    }
</script>